<?php

namespace common\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Wxuser;

/**
 * WxuserSearch represents the model behind the search form about `common\models\Wxuser`.
 */
class WxuserSearch extends Wxuser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'username', 'unionid', 'openid', 'wx_name', 'wx_images', 'password_reset_token', 'password_hash', 'auth_key', 'email', 'birthday', 'last_time', 'last_ip', 'qq', 'mobile_phone', 'nickname', 'blood', 'area'], 'safe'],
            [['wx_money', 'weight'], 'number'],
            [['status', 'created_at', 'updated_at', 'sex', 'address_id', 'rank_points', 'reg_time', 'last_login', 'visit_count', 'user_rank', 'is_special', 'age', 'height', 'country', 'province', 'login_status', 'city'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Wxuser::find();
//        $query->andWhere(['NOT', ['openid' => null]]);
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'wx_money' => $this->wx_money,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'sex' => $this->sex,
            'birthday' => $this->birthday,
            'address_id' => $this->address_id,
            'rank_points' => $this->rank_points,
            'reg_time' => $this->reg_time,
            'last_login' => $this->last_login,
            'last_time' => $this->last_time,
            'visit_count' => $this->visit_count,
            'user_rank' => $this->user_rank,
            'is_special' => $this->is_special,
            'age' => $this->age,
            'height' => $this->height,
            'weight' => $this->weight,
            'country' => $this->country,
            'province' => $this->province,
            'login_status' => $this->login_status,
            'city' => $this->city,
        ]);

        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'unionid', $this->unionid])
            ->andFilterWhere(['like', 'openid', $this->openid])
            ->andFilterWhere(['like', 'wx_name', $this->wx_name])
            ->andFilterWhere(['like', 'wx_images', $this->wx_images])
            ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'last_ip', $this->last_ip])
            ->andFilterWhere(['like', 'qq', $this->qq])
            ->andFilterWhere(['like', 'mobile_phone', $this->mobile_phone])
            ->andFilterWhere(['like', 'nickname', $this->nickname])
            ->andFilterWhere(['like', 'blood', $this->blood])
            ->andFilterWhere(['like', 'area', $this->area]);

        return $dataProvider;
    }
}
